﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MVCManage.BLL;
using MVCManage.Model;
using MVCManage.Api.Models;
using Microsoft.Ajax.Utilities;
using System.Diagnostics;

namespace MVCManage.Api.Controllers
{
    public class PderController : ApiController
    {

        
        [Route("api/Pder/Pdermain")]
        public ResultPageList<PderDto> mainGet()
        {


            var zz = new PderBLL().Query();
            var zz1 = new PderxqBLL().Query();
            zz1.Sum(n => n.price);
            var tt = from p in zz
                     select new PderDto
                     {
                         Pbh = p.pbh,
                         Dbh = p.dbh,
                         dizhi = p.dizhi,
                         UserID = p.userID,
                         Pdata = p.pdata,
                         Wcdata=p.wcdata,
                         Zhifu = p.zhifu,
                         Ptype =p.ptype,
                         Sdizhi=p.sdizhi,
                         zt = p.zt,
                         Phone = p.Users.phone,
                         UserName = p.Users.userName,
                         
                         Price = (float)zz1.Where(n => n.pbh == p.pbh).Sum(n1 => n1.price *n1.shu),
                         pxq = from n in p.pderxq
                               select new PderxqDto {
                                   Pbh = n.pbh,
                                   Pxqid = n.pxqid,
                                   Price = (float)n.price,
                                   Gid = n.gId,
                                   Name = n.Goods.name,
                                   Images = n.Goods.images,
                                   Shu = (int)n.shu,
                                   Pl=n.pl,
                                   Sfpl=n.sfpl
                               }
                     };
            ResultPageList<PderDto> result = new ResultPageList<PderDto>();
            result.code = 1;
            result.msg = "ok";
            result.data = tt.ToList();
            return result;
        }
   
        // GET: api/Pder
       
        public ResultPageList<PderDto> Get(int page, int limit, int ddzt, int xdzt, DateTime Atime , DateTime BTime)
        {
            int count = 0;
         
            var zz = new PderBLL().GetPdersList( page, limit, out count, ddzt,  xdzt,Atime,BTime);
           
          
            var zz1 = new PderxqBLL().Query();
            zz1.Sum(n => n.price);
            var tt = from p in zz
                     select new PderDto
                     {
                         Pbh = p.pbh,
                         Dbh = p.dbh,
                         dizhi = p.dizhi,
                         UserID = p.userID,
                         Pdata = p.pdata,
                         Zhifu = p.zhifu,
                         Ptype=p.ptype,
                         zt = p.zt,
                         Sdizhi=p.sdizhi,
                         Wcdata=p.wcdata,
                         Phone = p.Users.phone,
                         UserName = p.Users.userName,
                         
                         Price = (float)zz1.Where(n => n.pbh ==p.pbh).Sum(n1=>n1.price * n1.shu),
                         
                     };
            ResultPageList<PderDto> result = new ResultPageList<PderDto>();
            result.code = 0;
            result.count = count;
            result.msg = "ok";
            result.data = tt.ToList();
            return result;
        }
        [HttpGet]
        [Route("api/Pder/Pderfen")]
        public ResultPageList<PderDto> Get(int page, int limit,string name,DateTime sj1,DateTime sj2)
        {
            int count = 0;
            
            var zz = new PderBLL().chay(page, limit, out count, name,sj1,sj2);
            var zz1 = new PderxqBLL().Query();
            zz1.Sum(n => n.price);
            var tt = from p in zz
                     select new PderDto
                     {
                         Pbh = p.pbh,
                         Dbh = p.dbh,
                         dizhi = p.dizhi,
                         UserID = p.userID,
                         Pdata = p.pdata,
                         Zhifu = p.zhifu,
                         Ptype = p.ptype,
                         zt = p.zt,
                         Wcdata = p.wcdata,
                         Sdizhi=p.sdizhi,
                         Phone = p.Users.phone,
                         UserName = p.Users.userName,
                         Price = (float)zz1.Where(n => n.pbh == p.pbh).Sum(n1 => n1.price * n1.shu),
                         pxq = from n in p.pderxq
                               select new PderxqDto
                               {
                                   Pbh = n.pbh,
                                   Pxqid = n.pxqid,
                                   Price = (float)n.price,
                                   Gid = n.gId,
                                   Name = n.Goods.name,
                                   Images = n.Goods.images,
                                   Shu = (int)n.shu,
                                   Pl = n.pl,
                                   Sfpl = n.sfpl
                               }
                     };
            ResultPageList<PderDto> result = new ResultPageList<PderDto>();
            result.code = 0;
            result.count = count;
            result.msg = "ok";
            result.data = tt.ToList();
            return result;
        }

        // GET: api/Pder/5
        public string Get(int id)
        {
            return "value";
        }
        [Route("api/PderAdd")]
        public Result PostAddByGd([FromBody] predaddDto value)
        {
            var cc = new PderBLL().Query().Max(n => n.dbh);
            string tt = new PderBLL().hhh();
            pder pr = new pder();
         
            pr.pbh = tt;
            pr.pdata = DateTime.Now;
            pr.dizhi = "在线支付";
            pr.zhifu = "微信";
            pr.dbh = ((int.Parse(cc) + 1).ToString());
            pr.userID = value.UserID;
            pr.zt = 1;
            pr.ptype =1;
            pr.wcdata = DateTime.Now;
            pr.pderxq = new List<pderxq>() { };
            foreach (predaddyDto py in value.predkey)
            {
                pderxq key = new pderxq()
                {
                    pxqid = 0,
                    pbh = pr.pbh,
                    price = py.Price,
                    gId = py.Gid,
                    shu = py.Shu,
                    sfpl = py.Sfpl,
                    pl = py.Pl,
                  
                };
                pr.pderxq.Add(key);
            }
            bool zz = new PderBLL().Add(pr);
            Result result = new Result();
            result.code = zz ? 1 : 0;
            result.msg = zz ? "添加成功" : "添加失败";
            return result;
        }

        // POST: api/Pder
        public Result Post([FromBody]predaddDto value)
        {
            string tt = new PderBLL().hhh();
            pder pr = new pder();
            pr.pbh = tt;
            pr.pdata = DateTime.Now;
            pr.dizhi = value.dizhi;
            pr.zhifu = value.Zhifu;
            pr.dbh = value.Dbh;
            pr.userID = value.UserID;
            pr.ptype = value.Ptype;
            pr.wcdata = DateTime.Now;
            pr.sdizhi = value.Sdizhi;
            pr.zt = 1;
            pr.pderxq = new List<pderxq>(){ };
            foreach (predaddyDto py in value.predkey)
            {
                pderxq key = new pderxq()
                {
                    pxqid = 0,
                    pbh = pr.pbh,
                    price = py.Price,
                    gId = py.Gid,
                    shu = py.Shu,
                    sfpl = py.Sfpl,
                    pl=py.Pl
                };
                pr.pderxq.Add(key);
            }
            bool zz = new PderBLL().Add(pr);
            Result result = new Result();
            result.code = zz ? 1 : 0;
            result.msg = zz ? "添加成功" : "添加失败";
            return result;
        }
        [HttpPut]
        [Route("api/Pder/gai")]
        public Result gai([FromBody] predaddDto value)
        {
            var zz = new PderBLL().Query().Where(n => n.pbh == value.Pbh).FirstOrDefault();
            zz.zt = value.zt;
            zz.wcdata = DateTime.Now;
            bool zz1 = new PderBLL().Update(zz);
            Result result = new Result();
            result.code = zz1 ? 1 : 0;
            result.msg = zz1 ? "修改成功" : "修改失败";
            return result;
        }

        // PUT: api/Pder/5
        public Result Put([FromBody]predaddDto value)
        {
            pder pr = new pder();
            pr.pbh = value.Pbh;
            pr.pdata = value.Pdata;
            pr.dizhi = value.dizhi;
            pr.zhifu = value.Zhifu;
            pr.dbh = value.Dbh;
            pr.userID = value.UserID;
            pr.zt = 1;
            pr.pderxq = new List<pderxq>() { };
            foreach (predaddyDto py in value.predkey)
            {
                pderxq key = new pderxq()
                {
                    pxqid = 0,
                    pbh = pr.pbh,
                    price = py.Price,
                    gId = py.Gid,
                    shu = py.Shu,

                };
                pr.pderxq.Add(key);
            }
            bool zz = new PderBLL().Updatepder(pr);
            Result result = new Result();
            result.code = zz ? 1 : 0;
            result.msg = zz ? "修改成功" : "修改失败";
            return result;
        }

        [Route("api/Pder/delete")]
        // DELETE: api/Pder/5
        public Result Delete(string id)
        {
            bool pd = new PderBLL().Deletepder(id);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "删除成功" : "删除失败";
            return result;
        }

        [Route("api/Pder/AAA")]
        // DELETE: api/Pder/5
        public Result Put(ZTDto AA)
        {
            var  bo = new PderBLL().Query().Where(a=>a.pbh==AA.Pbh).FirstOrDefault();
            bo.zt = AA.Zt;
            var pd = new PderBLL().Update(bo);
            Result result = new Result();
            result.code = pd? 1 : 0;
            result.msg = pd ? "修改成功" : "删除失败";
            return result;
        }


    }
}
